package com.summer.shopping.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.summer.shopping.domain.common.req.ReqHolder;
import com.summer.shopping.domain.common.req.ReqInfo;
import com.summer.shopping.domain.table.HelpCenter;
import com.summer.shopping.domain.table.User;
import com.summer.shopping.domain.table.dto.AnswerDTO;
import com.summer.shopping.domain.table.dto.HelpCenterDTO;
import com.summer.shopping.domain.table.dto.HelpCenterQueryParam;
import com.summer.shopping.domain.table.dto.PageDTO;
import com.summer.shopping.domain.table.vo.HelpCenterVO;
import com.summer.shopping.fun.handlers.userId.GetUserIdHandler;
import com.summer.shopping.mapper.HelpCenterMapper;
import com.summer.shopping.mapper.UserMapper;
import com.summer.shopping.service.HelpCenterService;
import com.summer.shopping.utils.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:
 * Package:
 * Description:
 *
 * @Author: 陈三岁
 * @Create
 * @Version
 */
@Service
public class HelpCenterServiceImpl extends ServiceImpl<HelpCenterMapper, HelpCenter>
        implements HelpCenterService {

    @Autowired
    private HelpCenterMapper helpCenterMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<HelpCenterVO> pageQuery(PageDTO pageDTO, HelpCenterQueryParam param) {
        //设定排序字段
        Page<HelpCenter> helpCenterPage = new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize());
        OrderItem orderItem = new OrderItem().setAsc(false).setColumn("update_time");
        helpCenterPage.addOrder(orderItem);
        // 创建查询条件
        List<String> list = List.of(new String[]{"help_id","title","publisher_name","publisher_id","update_time"});
        QueryWrapper<HelpCenter> helpCenterQueryWrapper = new QueryWrapper<HelpCenter>().select(list);

        //添加条件
        if (BeanUtil.isNotEmpty(param)){
            if (param.getTitle() != null){
                //通过title中的关键字段查，如：登录异常问题，可以根据”登录“，或者”异常“查询到
                helpCenterQueryWrapper.like("title",param.getTitle());
            }
            if (param.getPublisherName() != null && param.getPublisherName().length() > 0){
                helpCenterQueryWrapper.like("publisher_name",param.getPublisherName());
            }
        }
        //查询
        Page<HelpCenter> helpCenterPage1 = baseMapper.selectPage(helpCenterPage, helpCenterQueryWrapper);
        //封装VO
        List<HelpCenter> records = helpCenterPage1.getRecords();
        List<HelpCenterVO> helpCenterVOS = new ArrayList<>();
        for (HelpCenter helpCenter: records) {
            helpCenterVOS.add(BeanUtil.copyProperties(helpCenter, HelpCenterVO.class));
        }

        PageResult<HelpCenterVO> helpCenterVOPageResult=new PageResult<>();
        //封装统一分页查询返回对象
        helpCenterVOPageResult.setRecords(helpCenterVOS);
        helpCenterVOPageResult.setTotal(helpCenterPage1.getTotal());
        return helpCenterVOPageResult;
    }


    /**
     * 新增问题
     * @param helpCenterDTO
     */
    public void saveHelpCenter(HelpCenterDTO helpCenterDTO) {
        HelpCenter helpCenter = new HelpCenter();
        //属性拷贝
        BeanUtils.copyProperties(helpCenterDTO,helpCenter);

        //设置更新时间，发布人，发布人id

        helpCenter.setUpdateTime(DateTime.now());
        helpCenter.setPublisherId(ReqHolder.getUserId());
        helpCenter.setPublisherName(userMapper.selectById(ReqHolder.getUserId()).getUsername());

        helpCenterMapper.insertProblem(helpCenter);
    }

    /**
     * 回答问题
     * @param answerDTO
     */

    public void answerHelpCenter(AnswerDTO answerDTO) {
        helpCenterMapper.updateProblemById(answerDTO);
    }

    /**
     * 删除问题
     * @param id
     */
    public void deleteById(Long id) {
        helpCenterMapper.deleteById(id);
    }
}
